package com.up.cloudfeiyibackend.service.impl;

import com.up.cloudfeiyibackend.constant.NewsMessageConstant;
import com.up.cloudfeiyibackend.entity.NewsCategory;
import com.up.cloudfeiyibackend.mapper.NewsMapper;
import com.up.cloudfeiyibackend.result.Result;
import com.up.cloudfeiyibackend.service.NewsService;
import com.up.cloudfeiyibackend.vo.NewsCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    /**
     *
     * 获取新闻分类
     * @return
     */
    @Override
    public Result<List<NewsCategoryVO>> getNewsCategory() {
        List<NewsCategory> newsCategory = newsMapper.getNewsCategory();
        if (newsCategory!= null && newsCategory.size() > 0) {
            List<NewsCategoryVO> newsCategoryVOList = new ArrayList<>();
            for (NewsCategory category : newsCategory) {
                NewsCategoryVO newsCategoryVO = new NewsCategoryVO();
                BeanUtils.copyProperties(category, newsCategoryVO);
                newsCategoryVOList.add(newsCategoryVO);
            }
            return Result.success(NewsMessageConstant.GET_NEWS_CATEGORY_SUCCESS,newsCategoryVOList);
        }
        return Result.error(NewsMessageConstant.GET_NEWS_CATEGORY_FAIL);
    }
}
